package com.ruoyi.materialCuttingPlan.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.materialCuttingPlan.domain.McpWindows;
import com.ruoyi.materialCuttingPlan.service.IMcpWindowsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.materialCuttingPlan.domain.McpProjects;
import com.ruoyi.materialCuttingPlan.service.IMcpProjectsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工程项目信息Controller
 * 
 * @author ruoyi
 * @date 2025-07-04
 */
@RestController
@RequestMapping("/material-cutting-plan/projects")
@Validated
public class McpProjectsController extends BaseController
{
    @Autowired
    private IMcpProjectsService mcpProjectsService;
    @Autowired
    private IMcpWindowsService mcpWindowsService;

    /**
     * 查询工程项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('material-cutting-plan:projects:list')")
    @GetMapping("/list")
    public TableDataInfo list(McpProjects mcpProjects)
    {
        startPage();
        List<McpProjects> list = mcpProjectsService.selectMcpProjectsList(mcpProjects);
        return getDataTable(list);
    }

    /**
     * 导出工程项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('material-cutting-plan:projects:export')")
    @Log(title = "工程项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, McpProjects mcpProjects)
    {
        List<McpProjects> list = mcpProjectsService.selectMcpProjectsList(mcpProjects);
        ExcelUtil<McpProjects> util = new ExcelUtil<McpProjects>(McpProjects.class);
        util.exportExcel(response, list, "工程项目信息数据");
    }

    /**
     * 获取工程项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('material-cutting-plan:projects:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mcpProjectsService.selectMcpProjectsById(id));
    }

    /**
     * 新增工程项目信息
     */
    @PreAuthorize("@ss.hasPermi('material-cutting-plan:projects:add')")
    @Log(title = "工程项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody McpProjects mcpProjects)
    {
        return toAjax(mcpProjectsService.insertMcpProjects(mcpProjects));
    }

    /**
     * 修改工程项目信息
     */
    @PreAuthorize("@ss.hasPermi('material-cutting-plan:projects:edit')")
    @Log(title = "工程项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody McpProjects mcpProjects)
    {
        return toAjax(mcpProjectsService.updateMcpProjects(mcpProjects));
    }

    /**
     * 删除工程项目信息
     */
    @PreAuthorize("@ss.hasPermi('material-cutting-plan:projects:remove')")
    @Log(title = "工程项目信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@Validated @PathVariable Long[] ids)
    {
        List<McpWindows> mcpWindows1 = mcpWindowsService.selectMcpWindowsListByProjectIds(ids);
        if (!mcpWindows1.isEmpty()){
            List<Long> windowIds = mcpWindows1.stream().map(McpWindows::getId).collect(Collectors.toList());
            Long[] array = new Long[windowIds.size()];
            array = windowIds.toArray(array);
            mcpWindowsService.deleteMcpWindowsByIds(array );
        }
        return toAjax(mcpProjectsService.deleteMcpProjectsByIds(ids));
    }
}
